﻿using System;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ESAM_Assignment_3
{
    public partial class ListComments : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string fullname = "";
            string name = Request.QueryString["username"];
            if (name != null)
            {
                fullname = UserClass.GetFullname(name);
                if ((string)Session[Common.ESAM_FULLNAME] != fullname)
                {
                    Response.Redirect("Default.aspx?Username=" + Session["username"]);
                }
            }
            else
            {
                
            }
            //Retrieve all comments from the database. Not in the PostBack as we want to keep retrieve any updated comment that may have been posted while user was reading.
            ArrayList result = CommentClass.ShowComments();
            foreach (CommentClass myComment in result)
            {
                string modified = "";

                if (myComment.Modified == "y")
                {
                    modified = "&nbsp;<img alt='[Comment has been updated]' title='Comment has been updated.' src='images/updated.png'>";
                }

                string currentUser;

                try
                {
                    currentUser = Request.QueryString["Username"];
                }
                catch
                {
                    currentUser = "";
                }

                string actions = "";
                if (currentUser == myComment.Username)
                {
                    string modifyComment = "<a href='EditComment.aspx?CommentID=" + myComment.CommentID + "&Username=" + currentUser + "' title='Edit Comment' alt='[Edit Comment]'><img src='images/edit.gif' border='0' hspace='3'></a>";
                    string deleteComment = "<a href='DeleteComment.aspx?CommentID=" + myComment.CommentID + "&Username=" + currentUser + "' title='Delete Comment' alt='[Delete Comment]'><img src='images/delete.gif' border='0' hspace='3'></a>";
                    //Session["CommentID"] = myComment.CommentID;
                    actions += modifyComment + deleteComment;
                }

                string messages = "";
                messages += "<table bgcolor='WHITE' border='0' cellpadding='3' cellspacing='0' width='60%' style='border-style: solid; border-width: thin'>";
                messages += "<tr><td bgcolor='SILVER'>Subject: " + myComment.Subject + "</td><td align='right' bgcolor='SILVER'>&nbsp;" + actions + "</td></tr>";

                messages += "<tr><td colspan='2'>" + myComment.Comment + "</td></tr>";
                messages += "<tr><td bgcolor='SILVER'>Posted By: " + myComment.Fullname + "</td><td align='RIGHT' bgcolor='SILVER'>Posted On: " + myComment.DatePosted + modified + "</td></tr>";
                messages += "</table><br>";

                lblListComment.Text += messages;
            }
        }
    }
}